﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class _789hiLogin : LoginImplBase
    {
        public _789hiLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = "http://www.789hi.com/reg/login.html";
            string data = String.Format("username={0}&userpass={1}&long=0&save=0", _username, _password);
            HttpResult resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.Contains("\"ok\":0"))
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            findingServerUrl();
            string serverlist_url = "http://www.789hi.com/astd.html";
            resp = TransferMgr.doGetPure(serverlist_url, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetServerList; return result; }
            string content = resp.getContent();
            Regex re = new Regex(String.Format("\"(astd_\\w+\\.html)\">双线{0}服</a>", _acc.ServerId));
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInGetServerList; return result; }
            string redirecturl = m.Groups[1].Value;
            makeSureValidUri(serverlist_url, ref redirecturl);
            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetServerList; return result; }
            content = resp.getContent();
            re = new Regex("<iframe.*?src=\"([^\"]*)\"");
            m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInGetServerList; return result; }
            redirecturl = m.Groups[1].Value;
            processStartGame(redirecturl, result, ref cookies);
            return result;
        }
    }
}
